#!/bin/sh

MAH='curl localhost:8080'
GRPNO=$1
DELAY=~/cache/lastsend

if echo $2 | grep -E '.mirai|png$'
then
    D={\"target\":$GRPNO,\"messageChain\":\[{\"type\":\"Image\",\"imageId\":\"$2\",\"path\":null,\"base64\":null}\]}
    echo $D
else
    TEXT=`echo "$2" | head -c 400` # shorter
    D={\"target\":$GRPNO,\"messageChain\":\[{\"type\":\"Plain\",\"text\":\"$TEXT\"}\]}
fi

sec ()
{
    date +%s
}

((NEXT=`cat $DELAY`+5))

while true
do
    echo Now `sec`, $NEXT
    if [ `sec` -gt $NEXT ]
    then
        sec > $DELAY
        break
    else
        echo sleeping to $NEXT "(`sec`)"
        sleep 2
    fi
done

echo `$MAH/sendGroupMessage -d "$D"`
